package com.book.manager.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.book.manager.entity.GiteeDTO;
import com.book.manager.entity.msg;
import com.book.manager.util.serviceUtil.ImgTagUtils;
import com.dingtalk.api.request.OapiRobotSendRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class SelectDingTemp {
@Autowired
giteeMsgHandler giteeMsgHandler;
@Autowired
MsgService msgService;
    /**
     * 抽取常用的第一层jsonKey减少代码冗余
     */
    private JSONObject getJson(String json) {
        return (JSONObject) JSON.parse(((JSONObject) JSON.parse(json)).getString("data"));
    }

    /**
     * 适配器
     */
    public OapiRobotSendRequest tempAdapterForDing(String json) throws IOException {
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        request.setMsgtype("actionCard");
        OapiRobotSendRequest.Actioncard actioncard = new OapiRobotSendRequest.Actioncard();
        actioncard.setBtnOrientation("1");
        List<String> s= ImgTagUtils.listImgSrc(getJson(json).getString("body_html"));
        actioncard.setText("> ![screenshot]("+s.get(0)+")\n");
        if (getJson(json).getString("published_at").equals(getJson(json).getString("first_published_at"))) {
            System.out.println("事件：首次发布，创建文档，文案：xxx公开了新的知识");
            //返回jsonTemp中的模板
            actioncard.setSingleTitle(((JSONObject) JSON.parse(getJson(json).getString("user"))).getString("name") + "发布了一篇文章");
            actioncard.setSingleURL("https://www.yuque.com/" + getJson(json).getString("path"));
            request.setActionCard(actioncard);

            msg msg = new msg();
            msg.setEvent("语雀创建新文章通知");
            msg.setJson(json);
            msg.setSpevent("创建");
            msg.setFrom1("语雀");
            msg.setHead(((JSONObject) JSON.parse(getJson(json).getString("user"))).getString("name"));
            msg.setEmail("1520553040@qq.com");
            msg.setTime(new Date());
            msg.setUrl("https://www.yuque.com/" + getJson(json).getString("path"));
            msgService.addMsg(msg);

            return request;
        } else if ("delete".equals(getJson(json).getString("action_type"))) {
            actioncard.setSingleTitle(((JSONObject) JSON.parse(getJson(json).getString("user"))).getString("name") + "删除了一篇文章");
            request.setActionCard(actioncard);

            msg msg = new msg();
            msg.setEvent("语雀删除文章通知");
            msg.setJson(json);
            msg.setSpevent("删除");
            msg.setFrom1("语雀");
            msg.setHead(((JSONObject) JSON.parse(getJson(json).getString("user"))).getString("name"));
            msg.setEmail("1520553040@qq.com");
            msg.setTime(new Date());
            msg.setUrl("https://www.yuque.com/" + getJson(json).getString("path"));
            msgService.addMsg(msg);

            return request;
        } else if ("update".equals(getJson(json).getString("action_type"))) {
            actioncard.setSingleTitle(((JSONObject) JSON.parse(getJson(json).getString("user"))).getString("name") + "更新了一篇文章");
            request.setActionCard(actioncard);

            msg msg = new msg();
            msg.setEvent("语雀更新文章通知");
            msg.setJson(json);
            msg.setSpevent("更新");
            msg.setFrom1("语雀");
            msg.setHead(((JSONObject) JSON.parse(getJson(json).getString("user"))).getString("name"));
            msg.setEmail("1520553040@qq.com");
            msg.setTime(new Date());
            msg.setUrl("https://www.yuque.com/" + getJson(json).getString("path"));
            msgService.addMsg(msg);

            return request;
        }
        return request;

    }

    public OapiRobotSendRequest tempAdapterForGitToDing(String json) throws IOException {
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        request.setMsgtype("actionCard");
        OapiRobotSendRequest.Actioncard actioncard = new OapiRobotSendRequest.Actioncard();
        actioncard.setBtnOrientation("1");

        boolean create = ((JSONObject) JSON.parse(json)).getBoolean("created");
        boolean delete = ((JSONObject) JSON.parse(json)).getBoolean("deleted");
        if(create){
            actioncard.setSingleTitle("Gitee代码仓库变动提醒:创建事件");
            GiteeDTO giteeDTO = giteeMsgHandler.gitToDingHandler(json);
            actioncard.setSingleURL(giteeDTO.getUrl());
            actioncard.setText(">变动分支："+giteeDTO.getBranch()+"\n"+">变动文件："+giteeDTO.getModified()+"\n"+">变动信息："+giteeDTO.getMessage()+"\n"
            +">变动人："+giteeDTO.getName()+"\n"+">变动时间："+giteeDTO.getTimestamp()+"\n"+">变动人邮箱："+giteeDTO.getEmail()+"\n"+">项目名："+giteeDTO.getProjectName()
            +"\n"+">项目归属："+giteeDTO.getOwner());

            msg msg = new msg();
            msg.setEvent("gitee分支变动通知："+giteeDTO.getBranch());
            msg.setJson(json);
            msg.setSpevent("创建");
            msg.setFrom1("gitee");
            msg.setHead(giteeDTO.getName());
            msg.setEmail(giteeDTO.getEmail());
            msg.setTime(new Date());
            msg.setUrl("https://gitee.com/wen_chen_lu/demo1");
            msgService.addMsg(msg);

        }else if(delete){
            actioncard.setSingleTitle("Gitee代码仓库变动提醒:删除事件");
            GiteeDTO giteeDTO = giteeMsgHandler.gitToDingHandler(json);
            actioncard.setSingleURL(giteeDTO.getUrl());
            actioncard.setText("####"+giteeDTO.getProjectName()+"####有一条分支被删除了"+"\n"+">项目归属："+giteeDTO.getOwner());
            msg msg = new msg();
            msg.setEvent("gitee分支变动通知：删除"+giteeDTO.getProjectName());
            msg.setJson(json);
            msg.setSpevent("删除");
            msg.setFrom1("gitee");
            msg.setHead(giteeDTO.getOwner());
            msg.setEmail("1520553040@qq.com");
            msg.setTime(new Date());
            msg.setUrl("https://gitee.com/wen_chen_lu/demo1");
            msgService.addMsg(msg);
        }else {
        actioncard.setSingleTitle("Gitee代码仓库变动提醒:更新事件");
        GiteeDTO giteeDTO = giteeMsgHandler.gitToDingHandler(json);
        actioncard.setSingleURL(giteeDTO.getUrl());
        actioncard.setText(">变动分支："+giteeDTO.getBranch()+"\n"+">变动文件："+giteeDTO.getModified()+"\n"+">变动信息："+giteeDTO.getMessage()+"\n"
                +">变动人："+giteeDTO.getName()+"\n"+">变动时间："+giteeDTO.getTimestamp()+"\n"+">变动人邮箱："+giteeDTO.getEmail()+"\n"+">项目名："+giteeDTO.getProjectName()
                +"\n"+">项目归属："+giteeDTO.getOwner());
            msg msg = new msg();
            msg.setEvent("gitee分支变动通知："+giteeDTO.getBranch());
            msg.setJson(json);
            msg.setSpevent("更新");
            msg.setFrom1("gitee");
            msg.setHead(giteeDTO.getName());
            msg.setEmail(giteeDTO.getEmail());
            msg.setTime(new Date());
            msg.setUrl("https://gitee.com/wen_chen_lu/demo1");
            msgService.addMsg(msg);
        }
        request.setActionCard(actioncard);
        return request;
    }



    /**
     * gitee-Feishu
     * */
    public  String richTextToFeishu(String json,String chatid){
        boolean create = ((JSONObject) JSON.parse(json)).getBoolean("created");
        boolean delete = ((JSONObject) JSON.parse(json)).getBoolean("deleted");
        if(create){
            GiteeDTO giteeDTO = giteeMsgHandler.gitToDingHandler(json);
            String richTextMsg = "{\n" +
                    "\"chat_id\": \""+chatid+"\",\n"+
                    "    \"msg_type\": \"post\",\n" +
                    "    \"content\": {\n" +
                    "        \"post\": {\n" +
                    "            \"zh_cn\": {\n" +
                    "                \"title\": \"Gitee代码仓库变动提醒:创建事件\",\n" +
                    "                \"content\": [\n" +
                    "                    [\n" +
                    "                        {\n" +
                    "                            \"tag\": \"text\",\n" +
                    "                            \"un_escape\": true,\n" +
                    "                            \"text\": \"项目名&nbsp;:\"\n" +
                    "                        },\n" +
                    "                        {\n" +
                    "                            \"tag\": \"a\",\n" +
                    "                            \"text\": \""+giteeDTO.getProjectName()+"\",\n" +
                    "                            \"href\": \""+giteeDTO.getUrl()+"\"\n" +
                    "                        }\n" +
                    "                    ],\n" +
                    "                    [\n" +
                    "                        {\n" +
                    "                            \"tag\": \"text\",\n" +
                    "                            \"text\": \"项目归属&nbsp; :\"\n" +
                    "                        },\n" +
                    "                        {\n" +
                    "                            \"tag\": \"text\",\n" +
                    "                            \"text\": \""+giteeDTO.getOwner()+"\"\n" +
                    "                        }\n" +
                    "                    ],\n" +
                    "                     [\n" +
                    "                        {\n" +
                    "                            \"tag\": \"text\",\n" +
                    "                            \"text\": \"变动分支&nbsp; :\"\n" +
                    "                        },\n" +
                    "                        {\n" +
                    "                            \"tag\": \"text\",\n" +
                    "                            \"text\": \""+giteeDTO.getBranch()+"\"\n" +
                    "                        }\n" +
                    "                    ],\n" +
                    "                      [\n" +
                    "                        {\n" +
                    "                            \"tag\": \"text\",\n" +
                    "                            \"text\": \"变动文件&nbsp; :\"\n" +
                    "                        },\n" +
                    "                        {\n" +
                    "                            \"tag\": \"text\",\n" +
                    "                            \"text\": \""+giteeDTO.getModified()+"\"\n" +
                    "                        }\n" +
                    "                    ],\n" +
                    "                      [\n" +
                    "                        {\n" +
                    "                            \"tag\": \"text\",\n" +
                    "                            \"text\": \"变动内容&nbsp; :\"\n" +
                    "                        },\n" +
                    "                        {\n" +
                    "                            \"tag\": \"text\",\n" +
                    "                            \"text\": \""+giteeDTO.getMessage()+"\"\n" +
                    "                        }\n" +
                    "                    ],\n" +
                    "                     [\n" +
                    "                        {\n" +
                    "                            \"tag\": \"text\",\n" +
                    "                            \"text\": \"变动时间&nbsp; :\"\n" +
                    "                        },\n" +
                    "                        {\n" +
                    "                            \"tag\": \"text\",\n" +
                    "                            \"text\": \""+giteeDTO.getTimestamp()+"\"\n" +
                    "                        }\n" +
                    "                    ],\n" +
                    "                     [\n" +
                    "                        {\n" +
                    "                            \"tag\": \"text\",\n" +
                    "                            \"text\": \"变动人&nbsp; :\"\n" +
                    "                        },\n" +
                    "                        {\n" +
                    "                            \"tag\": \"text\",\n" +
                    "                            \"text\": \""+giteeDTO.getName()+"\"\n" +
                    "                        }\n" +
                    "                    ]\n" +
                    "                ]\n" +
                    "            }\n" +
                    "        }\n" +
                    "    }\n" +
                    "}";
        }else if(delete){
            GiteeDTO giteeDTO = giteeMsgHandler.gitToDingHandler(json);
            String richTextMsg = "{\n" +
                    "\"chat_id\": \""+chatid+"\",\n"+
                    "    \"msg_type\": \"post\",\n" +
                    "    \"content\": {\n" +
                    "        \"post\": {\n" +
                    "            \"zh_cn\": {\n" +
                    "                \"title\": \"Gitee代码仓库变动提醒:创建事件\",\n" +
                    "                \"content\": [\n" +
                    "                    [\n" +
                    "                        {\n" +
                    "                            \"tag\": \"text\",\n" +
                    "                            \"un_escape\": true,\n" +
                    "                            \"text\": \"项目名&nbsp;:\"\n" +
                    "                        },\n" +
                    "                        {\n" +
                    "                            \"tag\": \"a\",\n" +
                    "                            \"text\": \""+giteeDTO.getProjectName()+"\",\n" +
                    "                            \"href\": \"http://www.feishu.cn\"\n" +
                    "                        }\n" +
                    "                    ]\n" +
                    "                ]\n" +
                    "            }\n" +
                    "        }\n" +
                    "    }\n" +
                    "}";
        }else {
            GiteeDTO giteeDTO = giteeMsgHandler.gitToDingHandler(json);
            String richTextMsg = "{\n" +
                    "\"chat_id\": \""+chatid+"\",\n"+
                    "    \"msg_type\": \"post\",\n" +
                    "    \"content\": {\n" +
                    "        \"post\": {\n" +
                    "            \"zh_cn\": {\n" +
                    "                \"title\": \"Gitee代码仓库变动提醒:更新事件\",\n" +
                    "                \"content\": [\n" +
                    "                    [\n" +
                    "                        {\n" +
                    "                            \"tag\": \"text\",\n" +
                    "                            \"un_escape\": true,\n" +
                    "                            \"text\": \"项目名&nbsp;:\"\n" +
                    "                        },\n" +
                    "                        {\n" +
                    "                            \"tag\": \"a\",\n" +
                    "                            \"text\": \""+giteeDTO.getProjectName()+"\",\n" +
                    "                            \"href\": \""+giteeDTO.getUrl()+"\"\n" +
                    "                        }\n" +
                    "                    ],\n" +
                    "                    [\n" +
                    "                        {\n" +
                    "                            \"tag\": \"text\",\n" +
                    "                            \"text\": \"项目归属&nbsp; :\"\n" +
                    "                        },\n" +
                    "                        {\n" +
                    "                            \"tag\": \"text\",\n" +
                    "                            \"text\": \""+giteeDTO.getOwner()+"\"\n" +
                    "                        }\n" +
                    "                    ],\n" +
                    "                     [\n" +
                    "                        {\n" +
                    "                            \"tag\": \"text\",\n" +
                    "                            \"text\": \"变动分支&nbsp; :\"\n" +
                    "                        },\n" +
                    "                        {\n" +
                    "                            \"tag\": \"text\",\n" +
                    "                            \"text\": \""+giteeDTO.getBranch()+"\"\n" +
                    "                        }\n" +
                    "                    ],\n" +
                    "                      [\n" +
                    "                        {\n" +
                    "                            \"tag\": \"text\",\n" +
                    "                            \"text\": \"变动文件&nbsp; :\"\n" +
                    "                        },\n" +
                    "                        {\n" +
                    "                            \"tag\": \"text\",\n" +
                    "                            \"text\": \""+giteeDTO.getModified()+"\"\n" +
                    "                        }\n" +
                    "                    ],\n" +
                    "                      [\n" +
                    "                        {\n" +
                    "                            \"tag\": \"text\",\n" +
                    "                            \"text\": \"变动内容&nbsp; :\"\n" +
                    "                        },\n" +
                    "                        {\n" +
                    "                            \"tag\": \"text\",\n" +
                    "                            \"text\": \""+giteeDTO.getMessage()+"\"\n" +
                    "                        }\n" +
                    "                    ],\n" +
                    "                     [\n" +
                    "                        {\n" +
                    "                            \"tag\": \"text\",\n" +
                    "                            \"text\": \"变动时间&nbsp; :\"\n" +
                    "                        },\n" +
                    "                        {\n" +
                    "                            \"tag\": \"text\",\n" +
                    "                            \"text\": \""+giteeDTO.getTimestamp()+"\"\n" +
                    "                        }\n" +
                    "                    ],\n" +
                    "                     [\n" +
                    "                        {\n" +
                    "                            \"tag\": \"text\",\n" +
                    "                            \"text\": \"变动人&nbsp; :\"\n" +
                    "                        },\n" +
                    "                        {\n" +
                    "                            \"tag\": \"text\",\n" +
                    "                            \"text\": \""+giteeDTO.getName()+"\"\n" +
                    "                        }\n" +
                    "                    ]\n" +
                    "                ]\n" +
                    "            }\n" +
                    "        }\n" +
                    "    }\n" +
                    "}";

        }





//
//        String richTextMsg = "{\"email\":\"fanlv@bytedance.com\",\"msg_type\":\"post\"," +
//                "\"content\":{\"post\":{\"zh_cn\":{\"title\":\""+title+"\"," +
//                "\"content\":[[{\"tag\":\"text\",\"un_escape\":true,\"text\":\"第一行&nbsp;:\"}," +
//                "{\"tag\":\"a\",\"text\":\"超链接\",\"href\":\"http://www.feishu.cn\"}," +
//                "{\"tag\":\"at\",\"user_id\":\"ou_18eac85d35a26f989317ad4f02e8bbbb\"}]," +
//                "[{\"tag\":\"text\",\"text\":\"第二行 :\"},{\"tag\":\"text\",\"text\":\"文本测试\"}]," +
//                "[{\"tag\":\"img\",\"image_key\":\""+imgKey+"\",\"width\":300,\"height\":300}]]}}}}";
        return "";
    }
}